package net.sky.oa.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import net.sky.oa.util.ReflectionUtils;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Type;

/**
 *通知表
 *InfNotice. @author hond
 */
@Entity
@Table(name = "INF_NOTICE")//通知表
public class InfNotice implements java.io.Serializable {

	private static final long serialVersionUID = 5673991033245515351L;
	
	// Fields
	protected Long id;
	private String title;       //通知标题
	private String content;     //通知内容
	private Date createtime;    //发布时间
	private Integer rank;       //紧要程度
	private Integer istop;      //是否置顶：1是2否
	private Date entoptime;     //截止置顶时间
	private Integer issms;      //是否短信提示：1是2否
	private Integer isims;      //是否短消息提示：1是2否
	private String annexurl;    //附件地址
	private Integer ispublic;   //是否公开：1是2否
	private Integer readtimes;  //查阅次数
	private String reader;      //查阅人员姓名
	private Integer peoplecount;//查阅人数
	private Integer status;        //通知状态
	private Integer sort;          //类别
	private InfType type;       //通知类型
	private SysEmployee user;     //发布人
	private SysDept dept;       //发布部门
	
	//通知范围用户
	private Set<InfNoticeUser> noticeUsers;
	
	//vo fields
	private Long eid;
	private String ename;
	private String sex;

	// Constructors

	/** default constructor */
	public InfNotice() {
	}
	
	
	
    public InfNotice(Long id, String title,Integer istop, Date createtime, SysEmployee user) {
		super();
		this.id = id;
		this.title = title;
		this.istop=istop;
		this.createtime = createtime;
		this.user = user;
	}

	@Id
    @Column(name = "ID", unique = true, nullable = false)
    @SequenceGenerator(name="SQ_INFNOTICE_ID",sequenceName="SQ_INFNOTICE_ID",allocationSize=1)
	@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SQ_INFNOTICE_ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id){
    	this.id = id;
    }
	
	@Column(name = "TITLE", length = 60)
	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Lob
    @Basic(fetch = FetchType.EAGER)
    @Column(name = "CONTENT", columnDefinition = "CLOB", nullable = true)
    @Type(type ="org.springframework.orm.hibernate3.support.ClobStringType")
	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "CREATETIME", length = 7)
	public Date getCreatetime() {
		return this.createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}

	@Column(name = "RANK", precision = 1, scale = 0)
	public Integer getRank() {
		return this.rank;
	}

	public void setRank(Integer rank) {
		this.rank = rank;
	}

	@Column(name = "ISTOP", precision = 1, scale = 0)
	public Integer getIstop() {
		return this.istop;
	}

	public void setIstop(Integer istop) {
		this.istop = istop;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "ENTOPTIME", length = 7)
	public Date getEntoptime() {
		return this.entoptime;
	}

	public void setEntoptime(Date entoptime) {
		this.entoptime = entoptime;
	}

	@Column(name = "ISSMS", precision = 1, scale = 0)
	public Integer getIssms() {
		return this.issms;
	}

	public void setIssms(Integer issms) {
		this.issms = issms;
	}

	@Column(name = "ISIMS", precision = 1, scale = 0)
	public Integer getIsims() {
		return this.isims;
	}

	public void setIsims(Integer isims) {
		this.isims = isims;
	}

	@Column(name = "ANNEXURL", length = 100)
	public String getAnnexurl() {
		return this.annexurl;
	}

	public void setAnnexurl(String annexurl) {
		this.annexurl = annexurl;
	}

	@Column(name = "ISPUBLIC", precision = 1, scale = 0)
	public Integer getIspublic() {
		return this.ispublic;
	}

	public void setIspublic(Integer ispublic) {
		this.ispublic = ispublic;
	}

	@Column(name = "READTIMES", precision = 5, scale = 0)
	public Integer getReadtimes() {
		return this.readtimes;
	}

	public void setReadtimes(Integer readtimes) {
		this.readtimes = readtimes;
	}

	@Column(name = "READER", length = 4000)
	public String getReader() {
		return this.reader;
	}

	public void setReader(String reader) {
		this.reader = reader;
	}

	@Column(name = "PEOPLECOUNT", precision = 5, scale = 0)
	public Integer getPeoplecount() {
		return this.peoplecount;
	}

	public void setPeoplecount(Integer peoplecount) {
		this.peoplecount = peoplecount;
	}

	@Column(name = "STATUS", precision = 2, scale = 0)
	public Integer getStatus() {
		return this.status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@Column(name = "SORT", precision = 2, scale = 0)
	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "TYPE_ID")
	public InfType getType() {
		return this.type;
	}

	public void setType(InfType type) {
		this.type = type;
	}
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "USER_ID")
	public SysEmployee getUser() {
		return this.user;
	}

	public void setUser(SysEmployee user) {
		this.user = user;
	}
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "DEPT_ID")
	public SysDept getDept() {
		return this.dept;
	}

	public void setDept(SysDept dept) {
		this.dept = dept;
	}


	@OneToMany(fetch=FetchType.EAGER,mappedBy="notice")
	@Cascade(value={CascadeType.SAVE_UPDATE,CascadeType.DELETE_ORPHAN}) 
	public Set<InfNoticeUser> getNoticeUsers() {
		return noticeUsers;
	}

	public void setNoticeUsers(Set<InfNoticeUser> noticeUsers) {
		this.noticeUsers = noticeUsers;
	}
	
	
	@Transient
	public Long getEid() {
		return eid;
	}

	public void setEid(Long eid) {
		this.eid = eid;
	}

	@Transient
	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}
	
	@Transient
	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}


	@Transient
	public Set<SysEmployee> getNoticeEmployees() {
		Set<SysEmployee> bs=new HashSet<SysEmployee>();
		for(InfNoticeUser user:noticeUsers){
			bs.add(user.getEmployee());
		}
		return bs;
	}
	
	@Transient
	public void mergeByareaNamesIds(final Collection<Long> areaIds) {
		Set<Long> set=new HashSet<Long>();
		List<Long> list=new ArrayList<Long>();
		for(Iterator<Long> iter=areaIds.iterator();iter.hasNext();){
			Long id=iter.next();
			if(set.add(id)){
				list.add(id);
			}
		}
		areaIds.clear();
		areaIds.addAll(list);
		
		Set<InfNoticeUser> noticeUser=this.getNoticeUsers();
		if(noticeUser==null){
			noticeUser=new HashSet<InfNoticeUser>();
		}
		Iterator<InfNoticeUser> it= noticeUser.iterator();
		try {
			  while(it.hasNext()){
				 InfNoticeUser element=it.next();
				 Long id=element.getEmployee().getId();
				 if(!areaIds.contains(id)){
			    		it.remove();
			    	}else{
			    		areaIds.remove(id);
			    	}
			  }
			  for(Long id: areaIds){
				  InfNoticeUser noticeEmp=new InfNoticeUser();
				  SysEmployee user=new SysEmployee();
				  user.setId(id);
				  noticeEmp.setEmployee(user);
				  noticeEmp.setNotice(this);
				  noticeEmp.setIsread(2);
                  noticeEmp.setStatus(1);
                  noticeUser.add(noticeEmp);
			  }
			  this.setNoticeUsers(noticeUser);
		} catch (Exception e) {
			e.printStackTrace();
			throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
		}
	}
	
	

}